/*
 * @Author: your name
 * @Date: 2023-08-10 16:33:22
 * @LastEditTime: 2023-08-10 16:35:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \thermalCamera\showImage.cpp
 */
#include "show_image.h"
#include <string>
extern QQueue<QByteArray>global_queue;



void ShowImage::Show(){
    // 从队列中取出并处理二维数组
    if(!global_queue.isEmpty()) {
       qDebug()<<"showing";
       QByteArray img_array;
       // 桔梗的桶
       if(ui->radioButtonOS2_128->isChecked()){
           qDebug()<<"select OS2-128";
           img_array = global_queue.dequeue();
           // 将QByteArray转换为OpenCV可以处理的格式
           const char* data = img_array.constData();
           int size = img_array.size();

           // 使用cv::imdecode解码图像
           dst_pic = cv::imdecode(cv::Mat(1, size, CV_8UC1, (uchar*)data), cv::IMREAD_ANYCOLOR);
       }else{
            qDebug()<<"select OS1-32";
            img_array = global_queue.dequeue();
            img_array.resize(2448*2048);
            char* data = img_array.data();
            src_pic = cv::Mat(2048,2448,CV_8UC1,(uchar*)data);
            cv::cvtColor(src_pic,dst_pic,cv::COLOR_BayerRG2BGR);
       }


       if(!dst_pic.empty()){
           cv::cvtColor(dst_pic, dst_pic, cv::COLOR_BGR2RGB);
           // 转换为QImage
           QImage tmpImg = QImage(dst_pic.data, dst_pic.cols, dst_pic.rows, dst_pic.step, QImage::Format_RGB888).copy();
           // 自适应显示图像到显示框中
           this->ShowImageInGrap(&tmpImg);
       }
       else{
           qDebug()<<"imdecode error";
           ui->textEditDebug->append( "图片解码失败,传输损坏");
       }

   }
}

/**************************************************
@brief   : 在Grap框中自适应地显示图片
@input   ：none
@output  ：none
**************************************************/
void ShowImage::ShowImageInGrap(QImage* img) {


    int tmpWidth, tmpHeight;
    float imgRatio = float(img->width()) / img->height();
    qDebug()<<"width:"<<ui->graphicsView->width()<<",height:"<<ui->graphicsView->height()<<"\n";
    float graphicsRatio = float(ui->graphicsView->width()) / ui->graphicsView->height();
    if (imgRatio > graphicsRatio) {
        tmpWidth = int(ui->graphicsView->width());
        tmpHeight = int(ui->graphicsView->width() / imgRatio);
    }
    else {
        tmpWidth = int(ui->graphicsView->height() * imgRatio);
        tmpHeight = int(ui->graphicsView->height());
    }
    QImage copyImg = img->scaled(tmpWidth-5, tmpHeight-5, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    // 保存图像
//    Save(&copyImg);
    scene->clear();
    scene->addPixmap(QPixmap::fromImage(copyImg));
    ui->graphicsView->setScene(scene);
}

void ShowImage::Save(){
    static int index = 0;
    if(dst_pic.empty()){
        qDebug()<<"暂无图片可以保存";
        ui->textEditDebug->append( "暂无图片可以保存");
        return;
    }
    std::string file_name = "./" + std::to_string(index)+".jpg";
    cv::imwrite(file_name, dst_pic);
    qDebug()<<"No. "<<index<<" pic is saved.";
    ui->textEditDebug->append( "No."+QString::number(index)+"pic is saved.");
    index++;

}

void ShowImage::StartStopTimer(){
    if(this->ui->pushButtonShow->text() == "显示图片"){
        showTimer->start(400);
        this->ui->pushButtonShow->setText("停止显示");
    }
    else{
         showTimer->stop();
         this->ui->pushButtonShow->setText("显示图片");
    }
}

